#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using BO = PHSRAG.Insight21.BusinessObjects;
using PHSRAG.Insight21.InsightUtilities;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using AgreementConstants = PHSRAG.Insight21.Agreements.AgreementConstants;
using AgreementsConstants = PHSRAG.Insight21.InsightUtilities.Constants.AgreementsConstants;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Agreements
{
	/// <summary>
	/// Business class for Contract Details. 
	/// It supports loading of contract details data for the given 
	/// fund number for the logged in user. 
	/// </summary>
	public class ContractDetails: BO.BusinessObject
	{
		#region Constants (Private)
		private static string	ContractDetailsKey			= GenerateKey.MakeKey("ContractDetailsKey_2f461248-9dc3-4b5b-9af7-377bccc0530e",GenerateKey.Agreement);
		private const string	GetContractDetailsSPName		= "GetContractDetails";
		private const string	DomainUserIDParamName			= "@domainUserID";
		private const string	AgreementIdParamName				="@agreementId";
		private const string	FundNumberParamName				="@fundNumber";
		//Used in SendByEmail Functionality
		private const string InsertPageQuerySPName			= "InsertPageQuery";
		private const string InsertQueryParamName				= "@query";		
		#endregion	

		#region Enumerations (Private)
		// Enumerates the arguments required
		// for loading contract details.
		private enum LoadMethodArgs
		{
			DomainUserID,	
			AgreementId,
			FundNumber,
			Count
		}

		// Enumerates the tables in the data set 
		// for the Contract Details report
		private enum TableIndex
		{
			SubContract,
			RelatedProtocols,
			AssociatedAgreements,
			TermsandConditions,
			MileStones,
			FundInfo,
			Count
		}
		#endregion
		
		#region Public Properties
		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return ContractDetailsKey; } }
		#endregion

		#region Constructor
		/// <summary>
		/// Initializes the base class with the specified cache policy
		/// </summary>
		/// <param name="cachePolicy">Prevalent cache policy</param>
		public ContractDetails() : base(null)
		{

		}
		#endregion Constructor

		#region Class Methods
		/// <summary>
		/// This method is used in SendByEmail functionality. 
		/// It inserts the parameters required for the page and fetches the last inserted id.
		/// </summary>
		/// <returns>last inserted ID returned by the stored procedure</returns>
		public static int GetQueryId(string query)
		{
			return Convert.ToInt32(Utility.DataAccess.ExecuteScalar(Utility.DataAccess.GetConnectionString(), 
				InsertPageQuerySPName, Utility.DataAccess.BuildSqlParameterArray(InsertQueryParamName, 
				SqlDbType.VarChar, query)));
		}
		#endregion

		#region Business Object Methods

		/// <summary>
		/// Initializes and loads the datacontainer with contract details data
		/// The method expects 2 parameters in the params object array.
		/// 1)Domain user order id 
		/// 2)Fund Number
		/// </summary>
		/// <param name="connectionString">connection string pointing to the database to get data from</param>
		/// <param name="args">arguments to load the data for contract details</param>
		public override void Load(string connectionString, params object[] args)
		{
			if (!args.Length.Equals((int)LoadMethodArgs.Count))
				throw new Exception(String.Format("Contract Details load requires {0} parameters", 
					((int)LoadMethodArgs.Count).ToString()));
			try
			{
				 DataContainer = Utility.DataAccess.GetDataSet( connectionString, GetContractDetailsSPName,
					Utility.DataAccess.BuildSqlParameterArray
					(DomainUserIDParamName, SqlDbType.Int, args[(int)LoadMethodArgs.DomainUserID],
					 AgreementIdParamName, SqlDbType.Int, args[(int)LoadMethodArgs.AgreementId],
					 FundNumberParamName,SqlDbType.VarChar,((string)args[(int)LoadMethodArgs.FundNumber]== string.Empty)?(object)DBNull.Value : args[(int)LoadMethodArgs.FundNumber]));
				
				if(DataContainer.Tables.Count < (int)TableIndex.Count -1 )
					throw new Exception(@"Failed to load contract details.");
				else
				{
					DataContainer.Tables[(int)TableIndex.SubContract].TableName	= 
						AgreementsConstants.SubContractsTableName;
					DataContainer.Tables[(int)TableIndex.RelatedProtocols].TableName	= 
						AgreementsConstants.RelatedProtocolsTableName;
					DataContainer.Tables[(int)TableIndex.AssociatedAgreements].TableName = 
						AgreementsConstants.AssociatedAgreementsTableName;
					DataContainer.Tables[(int)TableIndex.TermsandConditions].TableName = 
						AgreementsConstants.TermsAndConditionsTableName;
					DataContainer.Tables[(int)TableIndex.MileStones].TableName = 
						AgreementsConstants.MileStonesTableName;
					//Check Whether the fundinfo datatable is available in the datacontainer.
					if(DataContainer.Tables.Count.Equals((int)TableIndex.Count))
					DataContainer.Tables[(int)TableIndex.FundInfo].TableName = 
						AgreementsConstants.FundInfoTableName;
				}
			}
			catch(Exception ex)
			{
				throw(new Exception(@"Error in loading Contract Details.", ex));
			}	
		}
		#endregion	Business Object Methods		
	}
}
